package sam.learn.biz.datasource.controller.dto;

import sam.learn.biz.datasource.repo.entity.DatasourceBrandEnum;
import sam.learn.biz.datasource.repo.entity.DatasourceInfo;

public class DataSourceInfoVo {

    /**
     * region_code
     */
    private String regionCode;

    /**
     * aliasName
     */
    private String aliasName;

    /**
     * username
     */
    private String user;

    /**
     * password
     */
    private String pwd;

    private String driverClassName;

    private String url;

    public String getRegionCode() {
        return regionCode;
    }

    public void setRegionCode(String regionCode) {
        this.regionCode = regionCode;
    }

    public String getAliasName() {
        return aliasName;
    }

    public void setAliasName(String aliasName) {
        this.aliasName = aliasName;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPwd() {
        return pwd;
    }

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public String getDriverClassName() {
        return driverClassName;
    }

    public void setDriverClassName(String driverClassName) {
        this.driverClassName = driverClassName;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    private DataSourceInfoVo(Builder builder) {
        regionCode = builder.regionCode;
        aliasName = builder.aliasName;
        user = builder.user;
        pwd = builder.pwd;
        driverClassName = builder.driverClassName;
        url = builder.url;
    }

    public static DataSourceInfoVo from(DatasourceInfo datasourceInfo) {
        DatasourceBrandEnum brandEnum = DatasourceBrandEnum.getDatasourceBrandEnumByKey(datasourceInfo.getBrand());
        return Builder.builder()
                .regionCode(datasourceInfo.getRegionCode())
                .aliasName(datasourceInfo.getAliasName())
                .user(datasourceInfo.getUser())
                .pwd(datasourceInfo.getPwd())
                .url(brandEnum.getUrl(datasourceInfo.getIp(), datasourceInfo.getPort(), datasourceInfo.getSchema()))
                .driverClassName(brandEnum.getDriveClassName())
                .build();
    }


    public static final class Builder {
        private String regionCode;
        private String aliasName;
        private String user;
        private String pwd;
        private String driverClassName;
        private String url;

        public Builder() {
        }

        public static Builder builder() {
            return new Builder();
        }

        public Builder regionCode(String val) {
            regionCode = val;
            return this;
        }

        public Builder aliasName(String val) {
            aliasName = val;
            return this;
        }

        public Builder user(String val) {
            user = val;
            return this;
        }

        public Builder pwd(String val) {
            pwd = val;
            return this;
        }

        public Builder driverClassName(String val) {
            driverClassName = val;
            return this;
        }

        public Builder url(String val) {
            url = val;
            return this;
        }

        public DataSourceInfoVo build() {
            return new DataSourceInfoVo(this);
        }
    }
}
